package org.nw.formula;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 * 公式抽象类 所有公式都从该类继承，
 * 
 * @author fangw
 */
public abstract class AbstractFormula implements IFormula {
	protected Log logger = LogFactory.getLog(getClass());
	private FormulaDao dao = null;
	private List<Object> args = new ArrayList<Object>();
	private String outArgName;
	private String formulaStr;
	public static String ArgName_noCache = "__nocache";

	public FormulaDao getDao() {
		return dao;
	}

	public void setDao(FormulaDao dao) {
		this.dao = dao;
	}

	public void addArgs(Object args) {
		this.args.add(args);
	}

	public List<Object> getArgs() {
		return args;
	}

	public String getFormulaStr() {
		return formulaStr;
	}

	public String getOutArgName() {
		return outArgName;
	}

	/**
	 * 获取公式执行结果 context必须是List<Map<String,Object>>
	 * 
	 * @param rowContext
	 *            行上下文
	 * @return
	 */
	public abstract Object getResult(Map<String, Object> rowContext);

	public void setArgs(List<Object> args) {
		this.args = args;
	}

	public void setFormulaStr(String formulaStr) {
		this.formulaStr = formulaStr;
	}

	public void setOutArgName(String outArgName) {
		this.outArgName = outArgName;
	}

	/**
	 * 获取公式名称 注:该方法并不对公式解析和执行造成影响，只是为了让公式对象描述更加清晰
	 */
	public String getFormulaName() {
		String name = this.getClass().getName();
		String p = "Formula_";
		return name.substring(name.indexOf(p) + p.length());
	}

}
